const Filters = require("../models/filters");

class FiltersCtl {
  async get(ctx) {
    const _Filters = await Filters.find();
    ctx.body = {
      code: 200,
      data: _Filters,
      msg: "success",
    };
  }

  async create(ctx) {
    const _Filters = await new Filters({
      name: ctx.request.body.name,
      value: ctx.request.body.value,
      pid: ctx.request.body.pid,
      id: ctx.request.body.id,
      createAt: new Date(),
      updateAt: new Date(),
    }).save();

    ctx.body = {
      code: 200,
      data: _Filters,
      msg: "success",
    };
  }
  async modify(ctx) {
    const _Filters = await Filters.findOneAndUpdate(
      {
        id: ctx.request.body.id,
      },
      {
        name: ctx.request.body.name,
        value: ctx.request.body.value,
        updateAt: new Date(),
      }
    );

    ctx.body = {
      code: 200,
      data: _Filters,
      msg: "success",
    };
  }

  async remove(ctx) {
    const id = ctx.params.id;
    await Filters.deleteOne({
      id: id,
    });

    ctx.body = {
      code: 200,
      msg: "success",
    };
  }
}

module.exports = new FiltersCtl();
